package com.le.enhance.class7;

import org.junit.Test;

import java.util.Stack;

/**
 * 给定两个有符号32位整数a和b，不能使用算术运算符，分别实现a和 b的
 * 加、减、乘、除 运 算
 */
public class Code_03_AddMinusMultiDivideByBit {
    Stack<Character> stack = new Stack<>();

    public static int add(int a, int b) {
        int sum = a;
        while (b != 0) {
            sum = a ^ b; // 和
            b = (a & b) << 1; // 进位
            a = sum;
        }
        return sum;
    }

    public static int minus(int a, int b) {
        return add(a, add(~b,1));
    }

    public static int multi(int a, int b){
        int res = 0;
        while (b != 0){
            if ((b & 1) != 0){
                res = add(res,a);
            }
            a <<= 1;
            b >>>= 1;
        }
        return res;
    }

    @Test
    public void test() {
        int a = 6;
        int b = 6;
        System.out.println(add(a, b));
        System.out.println(minus(a, b));
        System.out.println(multi(a, b));
    }


}
